package taweb.flex.air.fexBrowser.display3d{
	import alternativa.engine3d.core.Mesh;
	import alternativa.engine3d.materials.TextureMaterial;
	import alternativa.types.Texture;
	import alternativa.utils.BitmapUtils;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	

	public class TextureLoaderEnv extends Mesh {

		protected var bindings:Array;
		protected var loadingMessage:String;
		private var onLoadMethod:Function;
		private var currentTextureIndex:int;
		private var loadingAlpha:Boolean;
		private var loader:Loader;
		private var request:URLRequest;
		private var loaderContext:LoaderContext;

		public function TextureLoaderEnv(name:String = null) {
			super(name)
		}


		public function load(onLoadMethod:Function):String {
			if (bindings == null) {
				onLoadMethod.call();
			} else {
				this.onLoadMethod = onLoadMethod;
				request = new URLRequest();
				loaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
				loader = new Loader();
				loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
				loader.contentLoaderInfo.addEventListener(IOErrorEvent.NETWORK_ERROR, onLoadError);
				loader.contentLoaderInfo.addEventListener(IOErrorEvent.VERIFY_ERROR, onLoadError);
				currentTextureIndex = 0;
			}
			return loadingMessage;
		}

		private function onLoadError(e:ErrorEvent):void {
			currentTextureIndex++;
			loadingAlpha = false;
		}

		private function complete():void {
			onLoadMethod.call();
			loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onLoadError);
			loader.contentLoaderInfo.removeEventListener(IOErrorEvent.NETWORK_ERROR, onLoadError);
			loader.contentLoaderInfo.removeEventListener(IOErrorEvent.VERIFY_ERROR, onLoadError);
			onLoadMethod = null;
			loader = null;
			bindings = null;
		}
	}
}